package com.warehouse.controller;

import com.sun.net.httpserver.Authenticator;
import com.warehouse.dto.Result;
import com.warehouse.dto.ResultCode;
import com.warehouse.entity.Supply;
import com.warehouse.service.SupplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: warehouse
 * @description: 供应商控制层
 * @author: xiaojiu
 * @create: 2021-01-12 20:43
 */
@Api(tags = "供应商管理")
@Slf4j
@RestController
@RequestMapping("/api")
public class SupplyController {
    @Autowired(required = false)
    private SupplyService supplyService;

    /**
     * 查询供应商所有信息
     * @return
     */
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public Result selectAll(){

        List<Supply> supplys=supplyService.selectAll();

        if(supplys!=null){
            return new Result(ResultCode.SUCCESS,supplys);
        }
        return new Result(ResultCode.FAIL,null);
    }
    /**
     *删除供应商信息
     */
    @ApiOperation(value = "删除应商",httpMethod = "DELETE")
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public Result deleteSupplys(String id){
      log.info("-------------------"+id);
        boolean delsupply = supplyService.delsupply(id);
        if(delsupply){
           return new Result(ResultCode.SUCCESS) ;
        }
        return new Result(ResultCode.FAIL);
    }

    /**
     * 新增用户商信息
     * @param supply
     * @return
     */

    @ApiOperation(value = "新增供应商",httpMethod = "POST")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result addSupply(@RequestBody  Supply supply){
        log.info("------------------------------"+supply.getName());

        boolean a=supplyService.insertSupply(supply);
        if(a){
           return new Result(ResultCode.SUCCESS);
        }
        else {
            return new Result(ResultCode.FAIL);
        }

    }
    /**
     * 修改供应商信息
     */
    @ApiOperation(value = "修改供应商信息",httpMethod = "PUT")
    @RequestMapping(value = "/edit",method = RequestMethod.PUT)
    public Result editSupply(@RequestBody Supply supply){
        log.info("------------------------------"+supply.getName());
        boolean s=supplyService.editSupply(supply);
        if(s){
            return new Result(ResultCode.SUCCESS);
        }
        else {
            return new Result(ResultCode.FAIL);
        }

    }
    /**
     * 模糊查询
     */
    @ApiOperation(value = "按条件模糊查询",httpMethod = "PUT")
    @RequestMapping(value = "/findSupply",method = RequestMethod.PUT)
    public Result findSupply(@RequestBody Supply supply){
        List<Supply> supplies=supplyService.findSupply(supply);
        if(supplies!=null){
            return new Result(ResultCode.SUCCESS,supplies);
        }
        else {
            return new Result(ResultCode.FAIL);
        }

    }
}
